package cn.cs.yangtai.controller;

import cn.cs.yangtai.entity.Blog;
import cn.cs.yangtai.entity.Comminicate;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.entity.vo.ResponsePojo;
import cn.cs.yangtai.service.ComminicateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (Comminicate)表控制层
 *
 * @author makejava
 * @since 2024-12-12 09:22:52
 */
@RestController
@CrossOrigin
@Tag(name = "Ai问答用户交流记录")
@RequestMapping("comminicate")
public class ComminicateController {
    /**
     * 服务对象
     */
    @Resource
    private ComminicateService comminicateService;
    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "问答记录分页查询")
    public ResponsePojo<PageResult<Comminicate>> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageResult<Comminicate> result=comminicateService.queryByPage(cuttentPage,pageSize);
        return ResponsePojo.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Comminicate> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.comminicateService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Comminicate> add(Integer iUserId,String question,String answer) {
        Comminicate comminicate=new Comminicate();
        comminicate.setIUserId(iUserId);
        comminicate.setQuestion(question);
        comminicate.setAnswer(answer);
        comminicate.setIDate(new Date());
        return ResponseEntity.ok(this.comminicateService.insert(comminicate));
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Comminicate> edit(Integer iId,String question,String answer) {
        Comminicate comminicate=new Comminicate();
        comminicate.setIId(iId);
        comminicate.setQuestion(question);
        comminicate.setAnswer(answer);
        return ResponseEntity.ok(this.comminicateService.update(comminicate));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.comminicateService.deleteById(id));
    }

}

